#!/bin/sh

set -ex

die () { echo "$@" ; exit 1; }

unset NVM_NODEJS_ORG_MIRROR
unset NVM_IOJS_ORG_MIRROR

set +e # TODO: fix
: nvm.sh
\. ../../../nvm.sh
set -e

! nvm_get_mirror || die 'unknown release type did not error'
! nvm_get_mirror node || die 'unknown release type did not error'
! nvm_get_mirror iojs || die 'unknown release type did not error'
! nvm_get_mirror node foo || die 'unknown release type did not error'
! nvm_get_mirror iojs foo || die 'unknown release type did not error'

[ -z "$NVM_NODEJS_ORG_MIRROR" ] || die "MIRROR environment variables should not be exported"
[ -z "$NVM_IOJS_ORG_MIRROR" ] || die "MIRROR environment variables should not be exported"

[ "$(nvm_get_mirror node std)" = "https://nodejs.org/dist" ] || die "incorrect default node-std mirror"
[ "$(nvm_get_mirror iojs std)" = "https://iojs.org/dist" ] || die "incorrect default iojs-std mirror"

NVM_NODEJS_ORG_MIRROR="https://test-domain"
[ "$(nvm_get_mirror node std)" = "https://test-domain" ] || die "node-std mirror should respect NVM_NODEJS_ORG_MIRROR"
unset NVM_NODEJS_ORG_MIRROR

NVM_IOJS_ORG_MIRROR="https://test-domain"
[ "$(nvm_get_mirror iojs std)" = "https://test-domain" ] || die "iojs-std mirror should respect NVM_IOJS_ORG_MIRROR"
unset NVM_IOJS_ORG_MIRROR

testMirrors() {
  NVM_NODEJS_ORG_MIRROR="${1-}"
  ! nvm_get_mirror node std || die "NVM_NODEJS_ORG_MIRROR errors with command injection attempt (${1-})"
  [ "$(nvm_get_mirror node std)" = "" ] || die 'NVM_NODEJS_ORG_MIRROR is protected against command injection'

  NVM_IOJS_ORG_MIRROR="${1-}"
  ! nvm_get_mirror iojs std || die "NVM_IOJS_ORG_MIRROR errors with command injection attempt (${1-})"
  [ "$(nvm_get_mirror iojs std)" = "" ] || die 'NVM_IOJS_ORG_MIRROR is protected against command injection'
}

testMirrors '`do something bad`'
testMirrors 'https://nodejs.org/dist; xdg-open http://www.google.com;'
testMirrors 'https://nodejs.org/dist&&xdg-open http://www.google.com;'
testMirrors 'https://nodejs.org/dist|xdg-open http://www.google.com;'

# Test that awk URL validation rejects non-URL values
testMirrors 'not a url'
testMirrors 'ftp://wrong-scheme'
testMirrors 'http://'
testMirrors 'javascript:alert(1)'

# a query string can never work, since nvm appends a path to the mirror
testMirrors 'https://nodejs.org/dist?token=abc'
testMirrors 'https://nodejs.org/dist#frag'

testAcceptedMirrors() {
  NVM_NODEJS_ORG_MIRROR="${1-}"
  [ "$(nvm_get_mirror node std)" = "${1-}" ] || die "NVM_NODEJS_ORG_MIRROR should accept ${1-}"
  unset NVM_NODEJS_ORG_MIRROR

  NVM_IOJS_ORG_MIRROR="${1-}"
  [ "$(nvm_get_mirror iojs std)" = "${1-}" ] || die "NVM_IOJS_ORG_MIRROR should accept ${1-}"
  unset NVM_IOJS_ORG_MIRROR
}

# a private mirror on a non-default port is a URL, and must not be rejected
testAcceptedMirrors 'https://mirror.internal:8443/dist'
testAcceptedMirrors 'http://localhost:8080/dist'
# an IPv6 literal host, userinfo, percent-encoding, and `~` are all legal too
testAcceptedMirrors 'http://[::1]:8080/dist'
testAcceptedMirrors 'https://user:pw@nexus.corp.example.com/repository/nodejs-dist'
testAcceptedMirrors 'https://mirror.internal/a%20b/dist'
testAcceptedMirrors 'https://mirror.internal/~dist'
